package gew.beans;

import gew.data.DimensionValue;
import gew.data.SearchResultItem;
import gew.metadata.CustomSql;
import gew.metadata.Queries;
import gew.system.Constants;
import gew.system.Labels;
import gew.system.RequestParameter;
import gew.utils.SqlUtils;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

@ManagedBean(name = "seacrhBn")
@RequestScoped
public class SearchBean extends BaseBean {

    private final static HashMap<String, int[]> SECTIONS_HIER;
    private final static String NO_ID = "-1";
    private final static int MAX_COUNT = 10;

    static {
        SECTIONS_HIER = new HashMap<>();
        SECTIONS_HIER.put(Constants.SECTION_INDICATORS,
                new int[]{
                    Constants.HIER_GROUP_INDICATOR,
                    Constants.HIER_GROUP_COUNTRY
                });
        SECTIONS_HIER.put(Constants.SECTION_TRADE_SUMMARY,
                new int[]{
                    Constants.HIER_GROUP_REPORTER,
                    Constants.HIER_GROUP_COMMODITY_CLASSIFICATION
                });
        SECTIONS_HIER.put(Constants.SECTION_TRADE,
                new int[]{
                    Constants.HIER_GROUP_REPORTER,
                    Constants.HIER_GROUP_TRADE_FLOW,
                    Constants.HIER_GROUP_PARTNER,
                    Constants.HIER_GROUP_PERIOD,
                    Constants.HIER_GROUP_COMMODITY_CLASSIFICATION,
                    Constants.HIER_GROUP_COMMODITY
                });
        SECTIONS_HIER.put(Constants.SECTION_INDUSTRIES,
                new int[]{
                    Constants.HIER_GROUP_INDICATOR_INDUSTRY,
                    Constants.HIER_GROUP_COUNTRY
                });
    }

    private RequestParameter rp;

    String inclTreeStart;
    String hierGroupSection;
    String urlTreeSection;

    private ArrayList<SearchResultItem> results;

    @PostConstruct
    public void init() {
        HttpServletRequest r = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        rp = new RequestParameter(r, dvs);
        logRequest(r, Constants.SECTION_SEARCH, rp.getRequestURI());

        // <editor-fold defaultstate="collapsed" desc="Trees' urls">
        String urlTreeStart = rp.getContextPath()
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(Constants.SECTION_SEARCH)
                .concat(SqlUtils.VERTICAL_BAR)
                .concat(rp.getFilterText())
                .concat(SqlUtils.VERTICAL_BAR);

        inclTreeStart = rp.getHttpHeader()
                .concat(SqlUtils.SLASH)
                .concat(rp.getLocaleUrlCode())
                .concat(SqlUtils.SLASH)
                .concat(Constants.SECTION_TREE)
                .concat(SqlUtils.SLASH);

        urlTreeSection = urlTreeStart
                .concat(SqlUtils.AT)
                .concat(rp.getSearchSectionUrlCode())
                .concat(SqlUtils.AT)
                .concat(SqlUtils.VERTICAL_BAR);

        hierGroupSection = (new Integer(Constants.HIER_GROUP_SECTION)).toString();

        // </editor-fold>
        // <editor-fold defaultstate="collapsed" desc="Search results ids">
        ArrayList<Integer> hierGroups = new ArrayList<>();
        for (String searchSectionUrlCode : rp.getSearchSectionUrlCodes()) {
            for (int i = 0; i < SECTIONS_HIER.get(searchSectionUrlCode).length; i++) {
                if (!hierGroups.contains(SECTIONS_HIER.get(searchSectionUrlCode)[i])) {
                    hierGroups.add(SECTIONS_HIER.get(searchSectionUrlCode)[i]);
                }
            }
        }

        HashMap<Integer, List<Integer>> hierGroupResults = new HashMap<>();
        ArrayList<Integer> hierGroupDefaultResults = new ArrayList<>();
        for (Integer hierGroupId : hierGroups) {
            String q1 = CustomSql.getTree2SearchIds(hierGroupId, rp.getFilterText());
            List<String> searchResults = df.executeQueryToListOfString(q1);
            String[] ids = String.join(SqlUtils.COMMA, searchResults).split(SqlUtils.COMMA);
            hierGroupResults.put(hierGroupId, new ArrayList<>());
            Arrays.sort(ids);
            String prevId = SqlUtils.EMPTY;
            for (int i = 0; i < ids.length; i++) {
                if (!prevId.equals(ids[i])) {
                    prevId = ids[i];
                    if (!NO_ID.equals(ids[i])) {
                        hierGroupResults.get(hierGroupId).add(Integer.parseInt(ids[i]));
                    }
                }
            }
            if (hierGroupResults.get(hierGroupId).isEmpty()) {
                hierGroupResults.get(hierGroupId).addAll(getDefaultIds(hierGroupId));
                hierGroupDefaultResults.add(hierGroupId);
            }
        }

        // </editor-fold>
        results = new ArrayList<>();

        for (String sectionUrlCode : rp.getSearchSectionUrlCodes()) {
            switch (sectionUrlCode) {
                case Constants.SECTION_INDICATORS: {
                    results.addAll(getSearchResultItemsIndicators(
                            hierGroupResults.get(Constants.HIER_GROUP_INDICATOR),
                            hierGroupResults.get(Constants.HIER_GROUP_COUNTRY),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_INDICATOR),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_COUNTRY)
                    ));
                }
                break;
                case Constants.SECTION_INDUSTRIES: {
                    results.addAll(getSearchResultItemsIndustries(
                            hierGroupResults.get(Constants.HIER_GROUP_INDICATOR_INDUSTRY),
                            hierGroupResults.get(Constants.HIER_GROUP_COUNTRY),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_INDICATOR_INDUSTRY),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_COUNTRY)
                    ));
                }
                break;
                case Constants.SECTION_TRADE_SUMMARY: {
                    results.addAll(getSearchResultItemsTradeSummary(
                            hierGroupResults.get(Constants.HIER_GROUP_COMMODITY_CLASSIFICATION),
                            hierGroupResults.get(Constants.HIER_GROUP_REPORTER),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_COMMODITY_CLASSIFICATION),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_REPORTER)
                    ));
                }
                break;
                case Constants.SECTION_TRADE: {
                    results.addAll(getSearchResultItemsTrade(
                            hierGroupResults.get(Constants.HIER_GROUP_REPORTER),
                            hierGroupResults.get(Constants.HIER_GROUP_TRADE_FLOW),
                            hierGroupResults.get(Constants.HIER_GROUP_PARTNER),
                            hierGroupResults.get(Constants.HIER_GROUP_PERIOD),
                            hierGroupResults.get(Constants.HIER_GROUP_COMMODITY_CLASSIFICATION),
                            hierGroupResults.get(Constants.HIER_GROUP_COMMODITY),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_REPORTER),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_TRADE_FLOW),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_PARTNER),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_PERIOD),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_COMMODITY_CLASSIFICATION),
                            hierGroupDefaultResults.contains(Constants.HIER_GROUP_COMMODITY)
                    ));
                }
                break;
            }
        }
        Collections.sort(results);
    }

    private List<SearchResultItem> getSearchResultItemsIndicators(
            List<Integer> indicatorIds,
            List<Integer> countryIds,
            boolean isIndicatorDefault,
            boolean isCountryDefault) {
        ArrayList<SearchResultItem> r = new ArrayList<>();
        String section = dvs.getNameByUrlCode(
                Queries.SectionDimensionValue,
                Constants.SECTION_INDICATORS,
                rp.getLocale());
        int index = 1;
        for (Integer indicatorId : indicatorIds) {

            String indicatorName = isIndicatorDefault
                    ? Labels.get_UI_TITLE_INDICATORS(rp.getLocale())
                    : dvs.getNameById(Queries.IndicatorDimensionValue, indicatorId, rp.getLocale());
            String indicatorUom = isIndicatorDefault
                    ? SqlUtils.EMPTY
                    : dvs.getUomById(Queries.IndicatorDimensionValue, indicatorId, rp.getLocale());
            String countryNames = isCountryDefault
                    ? SqlUtils.EMPTY
                    : dvs.getAllNames(Queries.CountryDimensionValue, countryIds, rp.getLocale());
            String countryNamesGen = isCountryDefault
                    ? Labels.get_UI_TITLE_COUNTRIES(rp.getLocale())
                    : dvs.getAllNamesGen(Queries.CountryDimensionValue, countryIds, rp.getLocale());

            String title = IndicatorsBean.getPageTitle(
                    rp.getLocale(),
                    indicatorName,
                    indicatorUom,
                    countryNames,
                    false);
            String description = IndicatorsBean.getDescription(
                    rp.getLocale(),
                    indicatorName,
                    countryNamesGen);
            String url = IndicatorsBean.getUrl(
                    rp.getContextPath(),
                    rp.getLocaleUrlCode(),
                    dvs.getUrlCodeById(Queries.IndicatorDimensionValue, indicatorId, rp.getLocale()),
                    dvs.getUrlCodes(Queries.CountryDimensionValue, countryIds, rp.getLocale(), SqlUtils.HYPHEN)
            );
            int score = (isIndicatorDefault ? 0 : 5) + (isCountryDefault ? 0 : 5);
            SearchResultItem item = new SearchResultItem(title, description, url, section, index, score);
            index++;
            r.add(item);
        }
        return r;
    }

    private List<SearchResultItem> getSearchResultItemsIndustries(
            List<Integer> indicatorIds,
            List<Integer> countryIds,
            boolean isIndicatorDefault,
            boolean isCountryDefault) {
        ArrayList<SearchResultItem> r = new ArrayList<>();
        String section = dvs.getNameByUrlCode(
                Queries.SectionDimensionValue,
                Constants.SECTION_INDUSTRIES,
                rp.getLocale());
        int index = 1;
        for (Integer indicatorId : indicatorIds) {

            String indicatorName = isIndicatorDefault
                    ? Labels.get_UI_TITLE_INDUSTRIES(rp.getLocale())
                    : dvs.getNameById(Queries.IndicatorDimensionValue, indicatorId, rp.getLocale());
            String indicatorUom = isIndicatorDefault
                    ? SqlUtils.EMPTY
                    : dvs.getUomById(Queries.IndicatorDimensionValue, indicatorId, rp.getLocale());
            String countryNames = isCountryDefault
                    ? SqlUtils.EMPTY
                    : dvs.getAllNames(Queries.CountryDimensionValue, countryIds, rp.getLocale());
            String countryNamesGen = isCountryDefault
                    ? Labels.get_UI_TITLE_COUNTRIES(rp.getLocale())
                    : dvs.getAllNamesGen(Queries.CountryDimensionValue, countryIds, rp.getLocale());

            String title = IndustriesBean.getPageTitle(
                    rp.getLocale(),
                    indicatorName,
                    indicatorUom,
                    countryNames,
                    false);
            String description = IndustriesBean.getDescription(
                    rp.getLocale(),
                    indicatorName,
                    countryNamesGen);
            String url = IndustriesBean.getUrl(
                    rp.getContextPath(),
                    rp.getLocaleUrlCode(),
                    dvs.getUrlCodeById(Queries.IndicatorDimensionValue, indicatorId, rp.getLocale()),
                    dvs.getUrlCodes(Queries.CountryDimensionValue, countryIds, rp.getLocale(), SqlUtils.HYPHEN)
            );
            int score = (isIndicatorDefault ? 0 : 5) + (isCountryDefault ? 0 : 5);
            SearchResultItem item = new SearchResultItem(title, description, url, section, index, score);
            index++;
            r.add(item);
        }
        return r;
    }

    private List<SearchResultItem> getSearchResultItemsTradeSummary(
            List<Integer> classificationIds,
            List<Integer> reporterIds,
            boolean isСlassificationDefault,
            boolean isReporterDefault) {
        ArrayList<SearchResultItem> r = new ArrayList<>();
        String section = dvs.getNameByUrlCode(
                Queries.SectionDimensionValue,
                Constants.SECTION_TRADE_SUMMARY,
                rp.getLocale());
        int index = 1;

        String reporterNames = isReporterDefault
                ? SqlUtils.EMPTY
                : dvs.getAllNames(Queries.CountryDimensionValue, reporterIds, rp.getLocale());

        String reporterNamesGen = isReporterDefault
                ? SqlUtils.EMPTY
                : dvs.getAllNamesGen(Queries.CountryDimensionValue, reporterIds, rp.getLocale());

        for (Integer classificationId : classificationIds) {
            String title = TradeSummaryBean.getTitle(
                    false,
                    rp.getLocale(),
                    reporterNames);
            String description = TradeSummaryBean.getDescription(
                    rp.getLocale(),
                    reporterNamesGen,
                    dvs.getNameById(Queries.CommodityGroupTypeDimensionValue, classificationId, rp.getLocale()));
            String url = TradeSummaryBean.getUrl(
                    rp.getContextPath(),
                    rp.getLocaleUrlCode(),
                    dvs.getUrlCodeById(Queries.CommodityGroupTypeDimensionValue, classificationId, rp.getLocale()),
                    dvs.getUrlCodes(Queries.CountryDimensionValue, reporterIds, rp.getLocale(), SqlUtils.HYPHEN)
            );
            int score = (isСlassificationDefault ? 0 : 5) + (isReporterDefault ? 0 : 5);
            SearchResultItem item = new SearchResultItem(title, description, url, section, index, score);
            index++;
            r.add(item);
        }
        return r;
    }

    private List<SearchResultItem> getSearchResultItemsTrade(
            List<Integer> reporterIds,
            List<Integer> tradeFlowIds,
            List<Integer> partnerIds,
            List<Integer> periodIds,
            List<Integer> classificationIds,
            List<Integer> commodityIds,
            boolean isReporterDefault,
            boolean isTradeFlowDefault,
            boolean isPartnerDefault,
            boolean isPeriodDefault,
            boolean isСlassificationDefault,
            boolean isCommodityDefault) {
        ArrayList<SearchResultItem> r = new ArrayList<>();
        String section = dvs.getNameByUrlCode(
                Queries.SectionDimensionValue,
                Constants.SECTION_TRADE,
                rp.getLocale());
        int index = 1;

        // <editor-fold defaultstate="collapsed" desc="vars">
        String tradeFlowNames = dvs.getAllNames(
                Queries.TradeFlowDimensionValue,
                tradeFlowIds,
                rp.getLocale());
        List<String> tradeFlowNamesList = new ArrayList<>();
        for (Integer tradeFlowId : tradeFlowIds) {
            tradeFlowNamesList.add(dvs.getNameById(Queries.TradeFlowDimensionValue, tradeFlowId, rp.getLocale()));
        }
        String periodNames = dvs.getAllNames(Queries.PeriodDimensionValue, periodIds, rp.getLocale());

        List<DimensionValue> commodityValues
                = dvs.getSelectedValues(
                        commodityIds,
                        rp.getLocale(),
                        Queries.CommodityDimensionValue);

        List<Integer> reporterPartnerIds = new ArrayList<>();
        List<Integer> classificationCommodityIds = new ArrayList<>();

        // </editor-fold>
        for (Integer reporterId : reporterIds) {

            // <editor-fold defaultstate="collapsed" desc="vars reporter">
            String reporterName = isReporterDefault
                    ? SqlUtils.EMPTY
                    : dvs.getNameById(Queries.CountryDimensionValue, reporterId, rp.getLocale());
            String reporterNameGen = isReporterDefault
                    ? SqlUtils.EMPTY
                    : dvs.getNameGenById(Queries.CountryDimensionValue, reporterId, rp.getLocale());
            String reporterNameAcc = isReporterDefault
                    ? SqlUtils.EMPTY
                    : dvs.getNameAccById(Queries.CountryDimensionValue, reporterId, rp.getLocale());

            reporterPartnerIds.addAll(partnerIds);
            reporterPartnerIds.remove(reporterId);
            String partnerNames = isPartnerDefault
                    ? SqlUtils.EMPTY
                    : dvs.getAllNames(Queries.CountryDimensionValue, reporterPartnerIds, rp.getLocale());
            String partnerNamesGen = isPartnerDefault
                    ? SqlUtils.EMPTY
                    : dvs.getAllNamesGen(Queries.CountryDimensionValue, reporterPartnerIds, rp.getLocale());
            String partnerNamesAcc = isPartnerDefault
                    ? SqlUtils.EMPTY
                    : dvs.getAllNamesAcc(Queries.CountryDimensionValue, reporterPartnerIds, rp.getLocale());
            if (reporterPartnerIds.isEmpty()) {
                reporterPartnerIds.addAll(getDefaultIds(Constants.HIER_GROUP_PARTNER));
            }
            String partnerUrlCodes = dvs.getUrlCodes(
                    Queries.CountryDimensionValue, 
                    reporterPartnerIds, 
                    rp.getLocale(),
                    SqlUtils.HYPHEN);
            reporterPartnerIds.clear();

            // </editor-fold>
            
            for (Integer classificationId : classificationIds) {

                // <editor-fold defaultstate="collapsed" desc="vars classification">
                String classificationName = dvs.getNameById(
                        Queries.CommodityGroupTypeDimensionValue,
                        classificationId,
                        rp.getLocale());
                String classificationUrlCode = dvs.getUrlCodeById(
                        Queries.CommodityGroupTypeDimensionValue,
                        classificationId,
                        rp.getLocale());                
                for (DimensionValue dv : commodityValues) {
                    if (dv.getUrlCode().startsWith(classificationUrlCode)) {
                        classificationCommodityIds.add(dv.getId());
                    }
                    if (classificationCommodityIds.size() == MAX_COUNT) {
                        break;
                    }
                }
                String commodityNames = dvs.getAllNames(
                        Queries.CommodityDimensionValue,
                        classificationCommodityIds,
                        rp.getLocale());
                String commodityUrlCodes = dvs.getUrlCodes(
                        Queries.CommodityDimensionValue,
                        classificationCommodityIds,
                        rp.getLocale(),
                        SqlUtils.HYPHEN);
                classificationCommodityIds.clear();
                
                // </editor-fold>

                String title = TradeBean2.getTitle(
                        tradeFlowNames,
                        reporterName,
                        partnerNames);
                String description = TradeBean2.getDescription(
                        rp.getLocale(),
                        tradeFlowIds,
                        tradeFlowNamesList,
                        periodNames,
                        reporterNameGen,
                        reporterNameAcc,
                        partnerNamesGen,
                        partnerNamesAcc,
                        classificationName,
                        commodityNames);
                String url = TradeBean2.getUrl(
                        rp.getContextPath(),
                        rp.getLocaleUrlCode(),
                        dvs.getUrlCodeById(Queries.CountryDimensionValue, reporterId, rp.getLocale()),
                        dvs.getUrlCodes(Queries.TradeFlowDimensionValue, tradeFlowIds, rp.getLocale(), SqlUtils.HYPHEN),
                        partnerUrlCodes,
                        dvs.getUrlCodes(Queries.PeriodDimensionValue, periodIds, rp.getLocale(), SqlUtils.HYPHEN),
                        classificationUrlCode,
                        commodityUrlCodes
                );
                int score = (isReporterDefault ? 0 : 1)
                        + (isTradeFlowDefault ? 0 : 1)
                        + (isPartnerDefault ? 0 : 1)
                        + (isPeriodDefault ? 0 : 1)
                        + (isСlassificationDefault ? 0 : 1)
                        + (isCommodityDefault ? 0 : 5);
                SearchResultItem item = new SearchResultItem(title, description, url, section, index, score);
                index++;
                r.add(item);
            }
        }
        return r;
    }

    private List<Integer> getDefaultIds(int hierGroupId) {
        String urls;
        switch (hierGroupId) {
            case Constants.HIER_GROUP_COMMODITY:
                urls = Labels.get_PARAM_DEFAULT_COMMODITY_GROUP_TYPE_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.CommodityDimensionValue);
            case Constants.HIER_GROUP_COMMODITY_CLASSIFICATION:
                urls = Labels.get_PARAM_DEFAULT_COMMODITY_GROUP_TYPE_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.CommodityGroupTypeDimensionValue);
            case Constants.HIER_GROUP_COUNTRY:
                urls = Labels.get_PARAM_DEFAULT_COUNTRY_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.CountryDimensionValue);
            case Constants.HIER_GROUP_INDICATOR:
                urls = Labels.get_PARAM_DEFAULT_INDICATOR_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.IndicatorDimensionValue);
            case Constants.HIER_GROUP_INDICATOR_INDUSTRY:
                urls = Labels.get_PARAM_DEFAULT_INDICATOR_INDUSTRY_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.IndicatorDimensionValue);
            case Constants.HIER_GROUP_PARTNER:
                urls = Labels.get_PARAM_DEFAULT_PARTNER_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.CountryDimensionValue);
            case Constants.HIER_GROUP_PERIOD:
                urls = Labels.get_PARAM_DEFAULT_PERIOD_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.PeriodDimensionValue);
            case Constants.HIER_GROUP_REPORTER:
                urls = Labels.get_PARAM_DEFAULT_REPORTER_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.CountryDimensionValue);
            case Constants.HIER_GROUP_TRADE_FLOW:
                urls = Labels.get_PARAM_DEFAULT_TRADE_FLOW_CODE_VALUE(rp.getLocale());
                return dvs.getIdByUrlCode(rp.getLocale(), urls, Queries.TradeFlowDimensionValue);
        }
        List<Integer> result = new ArrayList<>();
        return result;
    }

    public String getInclTreeSectionInit() {
        StringBuilder buf = new StringBuilder();
        buf
                .append(inclTreeStart)
                .append(Constants.FILTER_REQUEST_INIT)
                .append(SqlUtils.SLASH)
                .append(Constants.TREE_TEMPLATE_NO_SPACES_NO_CODES)
                .append(SqlUtils.SLASH)
                .append(hierGroupSection)
                .append(SqlUtils.SLASH)
                .append(urlTreeSection)
                .append(SqlUtils.SLASH)
                .append(rp.getSearchSectionIdsHS())
                .append(SqlUtils.SLASH);
        return buf.toString();
    }

    public String getLocaleUrlCode() {
        return rp.getLocaleUrlCode();
    }

    public String getPageTitle() {
        StringBuilder buf = new StringBuilder();
        buf.append(rp.getFilterText())
                .append(SqlUtils.SPACE).append(SqlUtils.HYPHEN).append(SqlUtils.SPACE)
                .append(Labels.get_UI_TITLE_SEARCH_RESULTS(rp.getLocale()))
                .append(SqlUtils.SPACE).append(SqlUtils.HYPHEN).append(SqlUtils.SPACE)
                .append(Labels.get_UI_TITLE_MAIN(rp.getLocale()));
        return buf.toString();
    }

    public String getShortTitle() {
        StringBuilder buf = new StringBuilder();
        buf.append(rp.getFilterText())
                .append(SqlUtils.SPACE).append(SqlUtils.HYPHEN).append(SqlUtils.SPACE)
                .append(Labels.get_UI_TITLE_SEARCH_RESULTS(rp.getLocale()));
        return buf.toString();
    }

    public String getKeyWords() {
        StringBuilder buf = new StringBuilder();
        buf.append(rp.getFilterText())
                .append(SqlUtils.SPACE)
                .append(Labels.get_UI_TITLE_SEARCH_RESULTS(rp.getLocale()));
        return buf.toString();
    }

    public String getUI_FOOTER_HOME() {
        return Labels.get_UI_FOOTER_HOME(rp.getLocale());
    }

    public String getUI_TITLE_SECTIONS() {
        return Labels.get_UI_TITLE_SECTIONS(rp.getLocale());
    }

    public Iterable<SearchResultItem> getResults() {
        return results;
    }

}
